CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(libtoml)

FIND_PROGRAM(RAGEL ragel)
FIND_PACKAGE(PkgConfig)
PKG_CHECK_MODULES(PC_LIBICU icu-uc)
PKG_CHECK_MODULES(PC_CUNIT cunit)

SET(RAGEL_SRCS toml_parse.rl)

SET(CMAKE_INCLUDE_CURRENT_DIR TRUE)
INCLUDE_DIRECTORIES(${PC_LIBICU_INCLUDE_DIRS} ${PC_CUNIT_INCLUDE_DIRS})

SET(SRCS toml.h toml.c toml_private.h toml_private.c)

FOREACH(RAGEL_SRC ${RAGEL_SRCS})
	STRING(REPLACE ".rl" ".c" C_SRC ${RAGEL_SRC})
	ADD_CUSTOM_COMMAND(OUTPUT ${C_SRC}
		COMMAND ${RAGEL} -n -p -o ${C_SRC} ${RAGEL_SRC}
		DEPENDS ${RAGEL} ${RAGEL_SRC}
		COMMENT "Building ${C_SRC} from ${RAGEL_SRC}"
	)
	SET_SOURCE_FILES_PROPERTIES(${C_SRC} PROPERTIES GENERATED TRUE)
	LIST(APPEND SRCS ${C_SRC} ${RAGEL_SRC})
	SOURCE_GROUP("Ragel Files" ${RAGEL_SRC})
ENDFOREACH()

LINK_DIRECTORIES(${PC_LIBICU_LIBRARY_DIRS} ${PC_CUNIT_LIBRARY_DIRS})

ADD_LIBRARY(toml SHARED ${SRCS})
TARGET_LINK_LIBRARIES(toml ${PC_LIBICU_LIBRARIES})

ADD_EXECUTABLE(main main.c)
TARGET_LINK_LIBRARIES(main toml ${PC_LIBICU_LIBRARIES})
ADD_EXECUTABLE(test test.c)
TARGET_LINK_LIBRARIES(test toml ${PC_LIBICU_LIBRARIES} ${PC_CUNIT_LIBRARIES})
